#################################
###   Reproduction code for   ###
###   tables and figures in   ###
### "Institution's Knowledge" ###
###    Emily C. Ommundsen     ###
#################################


###########
###PREPARE DATA AND PACKAGES
###########
# Load packages
library(ggplot2)
library(stargazer)
library(lme4)
library(estimatr)

# Load data
load("CES.les.rdata")
load("CES.rdata")
load("CES.les.2.rdata")

# Remove aging and ethics
CES.les.test <- subset(CES.les, CES.les$legistorm_cmte_id != 681)
CES.les.test <- subset(CES.les.test, CES.les.test$legistorm_cmte_id != 691)
CES.les.test$cmte <- as.factor(CES.les.test$legistorm_cmte_id)

# Break committees into classes
CES.a <- subset(CES, class_factor == "A")
CES.a.test <- subset(CES.les.test, class_factor == "A")
CES.b <- subset(CES, class_factor == "B")
CES.c <- subset(CES, class_factor == "C")
CES.bc <- subset(CES, class_factor == "B" | class_factor == "C")

###########
###FIGURE 1
###########
ggplot(CES.a, aes(x = congress, y = cmte_name, fill = ces)) + 
  geom_tile() +
  scale_fill_gradient(low="gray95", high="black") +
  labs(x = "\nCongress", 
       y = "Committee\n", 
       fill = "Committee \nEffectiveness \nScore") +
  coord_fixed(ratio = 1) +
  scale_x_continuous(breaks = c(103, 105, 107, 109, 111, 113, 115)) +
  scale_y_discrete(limits = rev) +
  theme_classic(base_line_size = 0)

ggplot(CES.a, aes(x = congress, y = cmte_name, fill = years_seniorstaff_exp)) + 
  geom_tile() +
  scale_fill_gradient(low="gray95", high="black") +
  labs(x = "Congress", 
       y = "Committee", 
       fill = "Senior Staff Experience") +
  coord_fixed(ratio = 1) +
  scale_x_continuous(breaks = c(103, 105, 107, 109, 111, 113, 115)) +
  scale_y_discrete(limits = rev) +
  theme_classic(base_line_size = 0)

###########
###FIGURE 2
###########
ggplot(CES.b, aes(x = congress, y = cmte_name, fill = ces)) + 
  geom_tile() +
  scale_fill_gradient(low="gray95", high="black") +
  labs(x = "\nCongress", 
       y = "Committee\n", 
       fill = "Committee \nEffectiveness \nScore") +
  coord_fixed(ratio = 1) +
  scale_x_continuous(breaks = c(103, 105, 107, 109, 111, 113, 115)) +
  scale_y_discrete(limits = rev) +
  theme_classic(base_line_size = 0)

###########
###FIGURE 3
###########
ggplot(CES.c, aes(x = congress, y = cmte_name, fill = ces)) + 
  geom_tile() +
  scale_fill_gradient(low="gray95", high="black") +
  labs(x = "\nCongress", 
       y = "Committee\n", 
       fill = "Committee \nEffectiveness \nScore") +
  coord_fixed(ratio = 1) +
  scale_x_continuous(breaks = c(103, 105, 107, 109, 111, 113, 115)) +
  scale_y_discrete(limits = rev) +
  theme_classic(base_line_size = 0)

###########
###FIGURE 4
###########
ggplot(CES.les, aes(x = ces, y = chm_les, color = class_factor)) +
  geom_point() +
  geom_smooth(aes(x = ces, y = chm_les), method = lm, se = F) +
  scale_color_manual(name = "Committee Class",
                     values = c("A" = "gray4",
                                "B" = "gray40",
                                "C" = "gray70")) +
  xlim(0, 6) +
  ylim(0, 8) +
  theme_bw() +
  labs(x = "\nCommittee Effectiveness Score", 
       y = "Chair's Legislative Effectiveness Score\n")

###########
###TABLE 1
###########
# OLS w/o ethics and aging, w/ LES
ces.mod1 <- lm(ces ~ years_staff_exp + years_senator_exp + 
                 new_chm + new_rm + chm_les + class_factor,
               data = CES.les.test)

ces.mod1.1 <- lm(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + class_factor,
                 data = CES.les.test) 

# Create table
stargazer(ces.mod1, ces.mod1.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES",
                               "Class-B committee", "Class-C committee"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")  

###########
###FIGURE 5
###########
ggplot(ces.mod1, aes(x = years_staff_exp, y = ces)) +
  geom_smooth(method = lm, colour = "black") +
  geom_rug(sides = "b") +
  labs(x = "\nStaff Experience (Congress)", 
       y = "Committee Effectiveness Score\n") +
  coord_cartesian(ylim = c(0.0, 2)) +
  theme_classic()

###########
###FIGURE 6
###########
ggplot(ces.mod1.1, aes(x = years_juniorstaff_exp, y = ces)) +
  geom_smooth(method = lm, colour = "black") +
  geom_rug(sides = "b") +
  labs(x = "\nJunior Staff Experience (Congress)", 
       y = "Committee Effectiveness Score\n") +
  coord_cartesian(ylim = c(0.0, 2)) +
  theme_classic()

###########
###FIGURE 7
###########
ggplot(ces.mod1.1, aes(x = years_seniorstaff_exp, y = ces)) +
  geom_smooth(method = lm, colour = "black") +
  geom_rug(sides = "b") +
  labs(x = "\nSenior Staff Experience (Congress)", 
       y = "Committee Effectiveness Score\n") +
  coord_cartesian(ylim = c(0.0, 2)) +
  theme_classic()

###########
###APPENDIX MODELS
###########

###E1
# Clustered SEs
app.mod1 <- lm_robust(ces ~ years_staff_exp + years_senator_exp + 
                          new_chm + new_rm + chm_les + class_factor,
                        clusters = legistorm_cmte_id,
                        data = CES.les.test)

app.mod1.1 <- lm_robust(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                          new_chm + new_rm + chm_les + class_factor,
                        clusters = legistorm_cmte_id,
                        data = CES.les.test) 

# Create table
stargazer(app.mod1, app.mod1.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES",
                               "Class-B committee", "Class-C committee"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, clustered standard errors in parentheses.") 

###E2
app.mod2 <- lm(ces ~ years_staff_exp + years_senator_exp + 
                 new_chm + new_rm + chm_les + class_factor,
               data = CES.les)

app.mod2.1 <- lm(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + class_factor,
                 data = CES.les) 

# Create table
stargazer(app.mod2, app.mod2.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES",
                               "Class-B committee", "Class-C committee"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")   

###E3
CES.les.test2 <- subset(CES.les.test, congress >= 107)

app.mod3 <- lm(ces ~ years_staff_exp + years_senator_exp + 
                 new_chm + new_rm + chm_les + class_factor,
               data = CES.les.test2)

app.mod3.1 <- lm(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + class_factor,
                 data = CES.les.test2) 

# Create table
stargazer(app.mod3, app.mod3.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member",
                               "Chair's LES",
                               "Class-B committee", "Class-C committee"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")

###E4
app.mod4 <- lmer(ces ~ years_staff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + (1 | class_factor),
                 data = CES.les.test)

app.mod4.1 <- lmer(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                     new_chm + new_rm + chm_les + (1 | class_factor),
                   data = CES.les.test)

app.mod4.2 <- lmer(ces ~ years_staff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + (1 | class_factor/cmte),
                 data = CES.les.test)

app.mod4.3 <- lmer(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                     new_chm + new_rm + chm_les + (1 | class_factor/cmte),
                   data = CES.les.test)

# Isolate random effects for table
sd1 <- VarCorr(app.mod4)
sd.vec1 <- unlist(sd1[])
sd.vec1 <- format(round(sd.vec1, 3), nsmall = 3)

sd2 <- VarCorr(app.mod4.1)
sd.vec2 <- unlist(sd2[])
sd.vec2 <- format(round(sd.vec2, 3), nsmall = 3)

sd3 <- VarCorr(app.mod4.2)
sd.vec3 <- unlist(sd3[])
sd.vec3 <- format(round(sd.vec3, 3), nsmall = 3)

sd4 <- VarCorr(app.mod4.3)
sd.vec4 <- unlist(sd4[])
sd.vec4 <- format(round(sd.vec4, 3), nsmall = 3)

sd.vec5 <- c(sd.vec1[1], sd.vec2[1], sd.vec3[1], sd.vec4[4])

addt.rows <- list(c("Committee Class Standard Deviation", sd.vec5))

# Create table
stargazer(app.mod4, app.mod4.1, app.mod4.2, app.mod4.3,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)", 
                               "Senior staff experience (Congress)",
                               "Senator experience (Congress)", "New chair", "New ranking member",
                               "Chair's LES"),
          dep.var.labels = "Committee Effectiveness Score",
          add.lines = addt.rows)

#E5
app.mod5 <- lmer(ces ~ years_staff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les + (1 | class_factor),
                 data = CES.les) 

app.mod5.1 <- lmer(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                     new_chm + new_rm + chm_les + (1 | class_factor),
                   data = CES.les)

# Isolate random effects for table
sd1 <- VarCorr(app.mod5)
sd.vec1 <- unlist(sd1[])
sd.vec1 <- format(round(sd.vec1, 3), nsmall = 3)

sd2 <- VarCorr(app.mod5.1)
sd.vec2 <- unlist(sd2[])
sd.vec2 <- format(round(sd.vec2, 3), nsmall = 3)

sd.vec4 <- c(sd.vec1[1], sd.vec2[1])

addt.rows <- list(c("Committee Class Standard Deviation", sd.vec4))

# Create table
stargazer(app.mod5, app.mod5.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)", 
                               "Senior staff experience (Congress)",
                               "Senator experience (Congress)", "New chair", "New ranking member",
                               "Chair's LES"),
          dep.var.labels = "Committee Effectiveness Score",
          add.lines = addt.rows)

###E6
app.mod6 <- lm(ces ~ years_seniorstaff_exp + years_senator_exp + 
                  new_chm + new_rm + chm_les + class_factor,
                data = CES.les.test) 

app.mod6.1 <- lm(ces ~ years_juniorstaff_exp + years_senator_exp + 
                 new_chm + new_rm + chm_les + class_factor,
               data = CES.les.test) 

# Create table
stargazer(app.mod6, app.mod6.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Senior staff experience (Congress)", 
                               "Junior staff experience (Congress)",
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES",
                               "Class-B committee", "Class-C committee"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")  

###E7
app.mod7 <- lm(ces ~ years_staff_exp + years_senator_exp + 
                 new_chm + new_rm + chm_les,
               data = CES.a.test)

app.mod7.1 <- lm(ces ~ years_juniorstaff_exp + years_seniorstaff_exp + years_senator_exp + 
                   new_chm + new_rm + chm_les,
                 data = CES.a.test) 

# Create table
stargazer(app.mod7, app.mod7.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")  

###E8
app.mod8 <- lm(ces ~ full_exp_avg + years_senator_exp + new_chm + 
                   new_rm + chm_les + class_factor,
                 data = CES.les.2)

app.mod8.1 <- lm(ces ~ full_exp_junior_avg + full_exp_senior_avg + years_senator_exp + new_chm + 
                     new_rm + chm_les + class_factor,
                   data = CES.les.2)

# Create table
stargazer(app.mod8, app.mod8.1,
          title = "Committee Effectiveness",
          covariate.labels = c("Staff experience (Congress)", "Junior staff experience (Congress)",
                               "Senior staff experience (Congress)", 
                               "Senator experience (Congress)", "New chair", "New ranking member", 
                               "Chair's LES"),
          dep.var.labels = "Committee Effectiveness Score",
          font.size = "small",
          notes = "Note: Ordinary least squares estimation, standard errors in parentheses.")  

